package com.eagle.ant.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.eagle.ant.view.ChooseViewTask;
import com.eagle.ant.R;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Name:
 * User: eagle
 * Date: 2020/1/6
 * Desc: 地点数据
 */
public class SelectAdapter extends RecyclerView.Adapter<SelectAdapter.ViewHolder> {
    private Context mContext;
    private LayoutInflater mInflater;
    private List<String> mDataList;
    private ChooseViewTask mViewTask;

    public SelectAdapter(Context context) {
        this.mContext = context;
        mInflater = LayoutInflater.from(context);
    }

    public void setDataList(List<String> dataList) {
        this.mDataList = dataList;
    }

    public void setViewTask(ChooseViewTask viewTask) {
        this.mViewTask = viewTask;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(mInflater.inflate(R.layout.adapter_select_item_view, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        holder.mLocationTextView.setTag(position);
        holder.mLocationTextView.setText(mDataList.get(position));
        holder.mLocationTextView.setOnClickListener(onClickListener);
    }

    private View.OnClickListener onClickListener = (v -> {
        int position = (int) v.getTag();
        if (mViewTask != null) {
            mViewTask.selectAreaTask(position);
        }
    });

    @Override
    public int getItemCount() {
        return mDataList == null ? 0 : mDataList.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.id_location_textView)
        TextView mLocationTextView;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
